Shader "Example02/05片元反射着色器"
{
    Properties
    {
        _MainColor("MainColor",Color) =(1,1,1,1)
        _MainTex ("Texture", 2D) = "white" {}
        _Gloss("Gloss",Range(0.03,1)) =0.5

        [KeywordEnum (Phong,Blinn)] SPECULARTYPE("Specular光照模型", Float) = 0 //宏定义
    }
    SubShader
    {
        Tags {
            "RenderType"="Opaque"
            "LightMode"="ForwardBase"
        }
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #include "Lighting.cginc"
            #pragma multi_compile SPECULARTYPE_PHONG SPECULARTYPE_BLINN //宏定义

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float3 worldPos : TEXCOORD0;
                float2 texcoord : TEXCOORD1;
                fixed3 normal : NORMAL;
            };

            fixed4 _MainColor;
            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed _Gloss;

            v2f vert (appdata_base v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex);
                o.texcoord =TRANSFORM_TEX(v.texcoord,_MainTex);
                o.normal = v.normal;
                return o;
            }

            //--- Phong反射 ------------------------------
            fixed3 SpecularPhong(fixed3 lightColor,fixed4 specularCol, fixed3 viewDir, fixed3 reflectDir,fixed gloss)
            {
                return lightColor * specularCol.rgb * specularCol.a * pow(max(0,dot(viewDir,reflectDir)), gloss);
            }

            //--- Blinn-Phong反射  ------------------------------
            fixed3 SpecularBlinn(fixed3 lightColor,fixed4 specularCol,fixed3 worldNormal,fixed3 halfDir,fixed gloss)
            {
                return lightColor * specularCol.rgb * specularCol.a * pow(max(0,dot(worldNormal, halfDir)), gloss);
            }

            fixed4 frag (v2f i) : SV_Target
            {
                //固定的
                fixed3 worldNormal = normalize(UnityObjectToWorldNormal(i.normal)); // 法线 => 世界坐标
                fixed3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));   // 光源向量
                fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));     // 视角向量
                fixed3 reflectDir = normalize(reflect(-lightDir,worldNormal));      // 反射向量
                fixed3 halfDir = normalize(lightDir + viewDir);                     // 半角方向
                
                fixed3 col = tex2D(_MainTex, i.texcoord);

                fixed3 specularCol;
                #ifdef SPECULARTYPE_PHONG 
                    specularCol = SpecularPhong(_LightColor0,_MainColor,viewDir,reflectDir,_Gloss * 128);  // Phong反射
                #elif SPECULARTYPE_BLINN
                    specularCol = SpecularBlinn(_LightColor0,_MainColor,worldNormal,halfDir,_Gloss * 128);   // Blinn反射
                #endif

                col.rgb *= specularCol; // 混合计算
                return  fixed4(col,1);
            }
            ENDCG
        }
    }
}